package com.ostrich.note.ui.settings.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.ostrich.note.R;
import com.ostrich.note.base.BaseMvvmActivity;
import com.ostrich.note.databinding.ActivitySettingsTypeBinding;
import com.ostrich.note.entity.BaseResponseEntity;
import com.ostrich.note.ui.note.adapter.NoteTypeEditAdapter;
import com.ostrich.note.ui.note.entity.TypeEntity;
import com.ostrich.note.ui.note.views.TypeEditActivity;
import com.ostrich.note.ui.settings.adapter.SettingsTypeAdapter;
import com.ostrich.note.ui.settings.viewmodel.SettingsTypeViewModel;
import com.ostrich.note.utils.DialogUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class SettingsTypeActivity extends BaseMvvmActivity<ActivitySettingsTypeBinding, SettingsTypeViewModel>{
    private SettingsTypeAdapter mSettingsTypeAdapter;
    @Override
    protected void iniView() {
        mSettingsTypeAdapter = new SettingsTypeAdapter();
        binding.rv.setAdapter(mSettingsTypeAdapter);
        binding.rv.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    public int getLayout() {
        return R.layout.activity_settings_type;
    }

    @Override
    public void initData() {

        viewModel.notepadClassifiedData().observe(this, new Observer<BaseResponseEntity<TypeEntity>>() {
            @Override
            public void onChanged(BaseResponseEntity<TypeEntity> typeEntityBaseResponseEntity) {
                List<TypeEntity.ListBean> list = typeEntityBaseResponseEntity.getData().getList();
                mSettingsTypeAdapter.setData(list);
            }
        });
    }

    @Override
    public void initListener() {
        binding.clBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSettingsTypeAdapter.setItemChildClickListener(new NoteTypeEditAdapter.ItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                showPopupMenu(view,position);
            }
        });

        binding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.showTypeInsertDialog(SettingsTypeActivity.this, new DialogUtils.DialogListener() {
                    @Override
                    public void onCancelClick() {

                    }

                    @Override
                    public void onConfirmClick() {

                    }

                    @Override
                    public void onConfirmClick(String data) {
                        //用户输入的修改的数据
                        viewModel.insertNotepadType(data,345693).observe(SettingsTypeActivity.this, new Observer<BaseResponseEntity>() {
                            @Override
                            public void onChanged(BaseResponseEntity baseResponseEntity) {
                                if(baseResponseEntity.getCode() == 200){
                                    ToastUtils.showShort(baseResponseEntity.getMessage());
                                    TypeEntity.ListBean listBean = new TypeEntity.ListBean();
                                    listBean.setNum(0);
                                    listBean.setTitle(data);
                                    mSettingsTypeAdapter.insertData(listBean);

                                }
                            }
                        });






                    }

                    @Override
                    public void onCheckChanged(boolean isChecked) {

                    }
                });
            }
        });

    }
    //弹出菜单
    private void showPopupMenu(View view,int position) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.menu,popupMenu.getMenu());

        //反射设置图标显示为true
        try {
            Field[] fields = popupMenu.getClass().getDeclaredFields();
            for (Field field : fields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popupMenu);
                    Class classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                    Method setForceIcOns= classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                    setForceIcOns.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.delete:

                        viewModel.notepadClassDel(mSettingsTypeAdapter.getDataByPosition(position).getNotepad_id()).observe(SettingsTypeActivity.this, new Observer<BaseResponseEntity>() {
                            @Override
                            public void onChanged(BaseResponseEntity baseResponseEntity) {
                                int code = baseResponseEntity.getCode();
                                if(code == 200){
                                    ToastUtils.showShort("删除成功");
                                    mSettingsTypeAdapter.deleteData(position);
                                }
                            }
                        });




                        break;
                    case R.id.rename:
                        TypeEntity.ListBean data = mSettingsTypeAdapter.getDataByPosition(position);

                        DialogUtils.showTypeEditDialog(SettingsTypeActivity.this, data.getTitle(), new DialogUtils.DialogListener() {
                            @Override
                            public void onCancelClick() {

                            }

                            @Override
                            public void onConfirmClick() {

                            }

                            @Override
                            public void onConfirmClick(String title) {
                                //修改分类
                                viewModel.notepadTitleEdit(title,000000,data.getNotepad_id()).observe(SettingsTypeActivity.this, new Observer<BaseResponseEntity>() {
                                    @Override
                                    public void onChanged(BaseResponseEntity baseResponseEntity) {
                                        if(baseResponseEntity.getCode() == 200){
                                            ToastUtils.showShort("修改成功");
                                            mSettingsTypeAdapter.updateData(position,title);
                                        }



                                    }
                                });
                            }

                            @Override
                            public void onCheckChanged(boolean isChecked) {

                            }
                        });
                        break;
                }
                return true;
            }
        });
        popupMenu.show();
    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(SettingsTypeViewModel.class);
    }
}